package com.ljs.myspringboot.controller;


import com.ljs.myspringboot.entity.Student_boot;
import com.ljs.myspringboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/student")
public class StudentHandler {
    @Autowired
    StudentService studentService;

    @RequestMapping("/")
    public String toadd() {
// 去注册页面的方法
        System.out.println("热部署启动");

        return "register";
    }

    @RequestMapping("/selall")
    @ResponseBody
    public List<Student_boot> selall() {
//        添加学生的方法

        return studentService.selAllStudent(3, 5);
    }

    @RequestMapping("/sel")
    @ResponseBody
    public Student_boot sel() {
//        添加学生的方法
        System.out.println("查询成功");
        return studentService.selStudent("张三");
    }

    @RequestMapping("/add")
    public String add(MultipartFile picture, Student_boot student_boot, HttpServletRequest request) throws FileNotFoundException {

//MultipartFile picture 的名称不要和实体类的命名一样，与前端页面相同
        String picnames = picture.getOriginalFilename();
        System.out.println("一开始图片路径" + picnames);
        String suffix = picnames.substring(picnames.indexOf("."));
        String filename = UUID.randomUUID().toString() + suffix;
        System.out.println("图片路径" + filename);
//
        String path = "E:\\宏图学习之旅\\springboot\\springboot_ljs\\src\\main\\resources\\static\\img";
        String abpath = ClassUtils.getDefaultClassLoader().getResource("static/img").getPath();
//最好不要用中文路径
        String OTHER = ResourceUtils.getURL("static/img").getPath();
//        System.out.println("获取静态资源路径"+abpath);
        System.out.println("获取静态资源路径1" + OTHER);
        File file = new File(path, filename);
        if (!file.exists()) {//如果不存在的话，就要创建
            file.mkdir();
        }
        try {
            picture.transferTo(file);//将这个文件写入文件中
        } catch (IOException e) {
            e.printStackTrace();
        }
        student_boot.setPic(filename);
        //        添加学生的方法
        String img = "/img/" + filename;
        if (studentService.addStudent(student_boot)) {
            request.setAttribute("img", img);//传到主页面显示图片
            return "showImg";
        } else {
            //去注册失败页面
            return "register";
        }


    }

    @RequestMapping("/login1")
    public String login(Student_boot student, Model model) {
//        添加学生的方法
        Student_boot student1 = studentService.login(student.getName(), student.getPassword());
        if (student1 != null) {
            model.addAttribute("student", student1);
        }

        return "main";//去主页面
    }


}
